package com.example.wastes.admin.controller;


import com.example.wastes.admin.mapper.AdminMapper;
import com.example.wastes.admin.service.AdminService;
import com.example.wastes.common.pojo.Admin;
import com.example.wastes.common.util.JwtUtils;
import com.example.wastes.common.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin//跨境
@RequestMapping("/admin")
@Api("用户管理模块")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private AdminMapper adminMapper;
    @ApiOperation("admin查询所有")
    @GetMapping("/getAllAdmin")
    public R getAllAdmin(){
        return new R(true,adminService.list());
    }
    @ApiOperation("admin添加信息")
    @PostMapping("/saveAdmin")
    public R saveAdmin(@RequestBody Admin admin){
        return new R(adminService.save(admin));
    }
    @ApiOperation("admin更新信息")
    @PutMapping("/updateAdmin")
    public R updateAdmin(@RequestBody Admin admin){
        return new R(adminService.updateById(admin));
    }
    @ApiOperation("admin删除信息")
    @DeleteMapping("/deleteAdmin")
    public R deleteAdmin(Integer id){
        return new R(adminService.removeById(id));
    }

    @Autowired
    private JwtUtils jwtUtils;
    @ApiOperation("admin登录")
    @GetMapping("/login")
    public Object tologin(@RequestParam(value = "username") String username,@RequestParam(value = "password") String password){
        long userId=0;
        Map<String, Object> map = new HashMap<>();
        if(username == null || password == null){
            map.put("code",100);
            map.put("msg","请求参数不能为空");
        }else {
            Admin admin = adminService.tologin(username);
            if (admin !=null){
                if (password.equals(admin.getPassword())){
                    userId = 1;
                    String token= jwtUtils.generateToken(userId);
                    map.put("code",200);
                    map.put("msg","登陆成功");
                    map.put("token",token);
                    map.put("user",admin);
                }else {
                    map.put("code",103);
                    map.put("msg","用户名和密码错误");
                }
            }else{
                map.put("code",102);
                map.put("msg","该用户不存在");
            }
        }
        return map;
    }
    @ApiOperation("admin注册")
    @GetMapping("/register")
    public String register(String username,String password){
//        System.out.println(username);
//        System.out.println(password);
        if (StringUtils.isEmpty(username)){
            return "用户名不允许为空";
        }
        if (StringUtils.isEmpty(password)){
            return "密码不允许为空";
        }
        Admin admin = adminService.tologin(username);
        if (admin != null){
            return "添加失败，用户存在";
        }
            int resultCount = adminService.saveAdmin(username, password);
            System.out.println(resultCount);
            if (resultCount == 0) {
                return "添加失败";
            }
            return "添加成功";
    }
    @GetMapping("findAdminName")
    @ApiOperation("通过名字查询")
    public R findAdminName(String username){
        return new R(true,adminMapper.findAdminName(username));
    }
}
